//
//  StandardViewCell.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/6/17.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "StandardViewCell.h"
#import "StandButton.h"


@interface StandardViewCell()

@property(nonatomic,strong)StandButton *btn;

@property(nonatomic,strong) UIButton *selectedButton;

@property(nonatomic,strong)NSMutableArray *btnArray;


@end

@implementation StandardViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
    }
    return self;
}



- (void)setStandardArray:(NSArray *)standardArray {
    _standardArray = standardArray;
    
    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = [UIFont systemFontOfSize:13];
//    _titleLabel.text = @"款式";
    _nameLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:_nameLabel];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(10);
        make.left.equalTo(self.mas_left).offset(10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(15);
    }];
    
    
    int count = 0;
    float btnWidth = 0;
    float btnMargin = 10;
    self.btnArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < _standardArray.count; ++i) {
        _btn = [[StandButton alloc] init];
        [self.contentView addSubview:_btn];
        [_btn addTarget:self action:@selector(standardButtonDidclick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [_btn setTitle:[NSString stringWithFormat:@"%@",_standardArray[i]] forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        [_btn setBackgroundColor:[UIColor whiteColor]];

        _btn.layer.borderColor = [UIColor grayColor].CGColor;
        
        NSString *str = _standardArray [i];
        _btn.tag = i;
        NSDictionary *dict = [NSDictionary dictionaryWithObject:FONT(15) forKey:NSFontAttributeName];
        CGSize btnSize = [str sizeWithAttributes:dict];
        _btn.width = btnSize.width + WIDTH/20;
        _btn.Height = btnSize.height + btnMargin;
        
      
        [self.btnArray addObject:_btn];
        
        if (i == 0) {
            _btn.x = btnMargin;
            btnWidth += CGRectGetMaxX(_btn.frame);
        }else{
            btnWidth += CGRectGetMaxX(_btn.frame)+20;
            if (btnWidth > WIDTH) {
                count ++;
                _btn.x = btnMargin;
                btnWidth = CGRectGetMaxX(_btn.frame);
            }else{
                _btn.x += btnWidth - _btn.width;
            }
        }
        
        _btn.y += count *(_btn.Height+btnMargin)+30;
        
    }
    
    UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(0,_btn.y + _btn.Height + 15, WIDTH,1)];
    lineView2.backgroundColor = [UIColor blackColor];
    lineView2.alpha = 0.1;
    [self.contentView addSubview:lineView2];
 
}

- (void)standardButtonDidclick:(UIButton *)button {
    
    [self.selectedButton setBackgroundColor:[UIColor whiteColor]];
    self.selectedButton.layer.borderColor = [UIColor grayColor].CGColor;
  
    self.selectedButton.enabled = YES;
    button.enabled = false;
    self.selectedButton = button;
    
    [self.selectedButton setBackgroundColor:DEFAULT_COLOR];
    self.selectedButton.layer.borderColor = DEFAULT_COLOR.CGColor;

    NSString *selectStr = self.standardArray[button.tag];
//        NSLog(@"%@",selectStr);
    if ([self.delegate respondsToSelector:@selector(StandardViewCellDelegateClick:)]) {
        [self.delegate StandardViewCellDelegateClick:selectStr];
    }
}

- (CGFloat)heightForCell:(NSArray *)array
{

//    NSLog(@"%@",array);
    int count = 0;
    float btnWidth = 0;
    float btnMargin = 10;
    
    for (int i = 0; i < array.count; ++i) {
        _btn = [[StandButton alloc] init];
        [_btn setTitle:[NSString stringWithFormat:@"%@",array[i]] forState:UIControlStateNormal];
        [self.contentView addSubview:_btn];
        NSString *str = array [i];
        NSDictionary *dict = [NSDictionary dictionaryWithObject:FONT(15) forKey:NSFontAttributeName];
        CGSize btnSize = [str sizeWithAttributes:dict];
        
        _btn.width = btnSize.width + WIDTH/20;
        _btn.Height = btnSize.height + btnMargin;
        
        
        if (i == 0) {
            _btn.x = btnMargin;
            btnWidth += CGRectGetMaxX(_btn.frame);
        }else{
            btnWidth += CGRectGetMaxX(_btn.frame)+20;
            if (btnWidth > WIDTH) {
                count ++;
                _btn.x = btnMargin;
                btnWidth = CGRectGetMaxX(_btn.frame);
            }else{
                _btn.x += btnWidth - _btn.width;
            }
        }
        
        _btn.y += count *(_btn.Height+btnMargin)+30;
        
    }
    
    return _btn.y + _btn.Height + 15;
//    NSLog(@"%zd",_btn.y);
}



@end
